import Cookies from 'js-cookie'
import api from '@/http/api'
import router from '../../router';
// import router from ''

export default {
    state: {
        userInfo: {},
    },
    getters: {
        userInfo: state => state.userInfo,
        // userInfo: ()=>{
        //     Cookies.get("userInfo")
        // },
        token: () => {
            Cookies.get('token');
        }
    },
    mutations: {
        setUserInfo: (state, userInfo) => {
            state.userInfo = userInfo
        },
        // setUserInfo: (state, userInfo) => {
        //     Cookies.set("userInfo", {expires: new Date(new Date().getTime() + 5 * 60 * 1000)})
        // },

    },
    actions: {
        fetchUserInfo(context) {
            let token = Cookies.get('token')
            if (token) {
                api.user.fetchUserInfo({token: token}).then(res => {
                    if (res.code == 0) {
                        let userInfo = res.userInfo
                        context.commit('setUserInfo', userInfo)
                    }else if(res.code==-1){
                        Cookies.set('token',null,{expires: new Date(new Date().getTime()-1000)})
                        context.commit('setUserInfo', null)
                        router.push('/author')
                    }else{
                    //    console.log("未知异常!") 
                    //    router.push('/author')    
                    }
                })
            }
        }


    }
}
